import TableLyout from '@/components/commonPublic/tableLyoutDetail.vue'
import TableData from "@/components/public/el-table-pagination";
import CustomDialog from "@/components/stationProductionPage/BridgeManage/keyInformation/index.vue";
import CustomDialog1 from "@/components/stationProductionPage/BridgeManage/uavNum/index.vue";
import formJs from "./regexDetail.js";
import {
    getKeyInfoList, //列表页接口
    updateKeyInfoStatus, //提交
    deleteKeyInfo,//删除
} from "@/api/BackStage/stationProductionPage/request.js";

export default {
    name: "keyInformationText",
    data() {
        return {
            name: "keyInformationText",
            treelength: false, //左侧树显示隐藏
            isLock: false,
            loading: false,
            btnObj: [], //按钮

            // 列表
            getList: {
                fCompanyid: "", // 左侧选中组织机构id
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                userId: "", // 查询待办或已办时传 列表页查询不传
                modelType: 2 // 0-待办 1-已办 传2 表示列表页查询 
            },
            table_config: formJs.table_config, //列表
            queryCriteriaData: {}, //分页
            tableData: [],
            currentRow: null, //列表详情
            dataFid: null, //列表详情id
            // 列表

            // 弹框
            isShow: false, //是否显示弹框
            isShow1: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            // 弹框

        };
    },
    components: {
        TableLyout,
        TableData,
        CustomDialog,
        CustomDialog1
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.getList.fCompanyid = this.userInfo.companyId;
        this.yj_searchForm(this.getList);
        this.btnRole(this.name);
    },
    methods: {
        // 弹框
        refreshList() {
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
            this.isShow1 = false;
        },
        // 弹框
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },

        // 新增/编辑/提交/删除
        yj_edit(num) {
            if (this.userInfo.userId != 'System') {
                this.statusNum = num;
                if (num == 1) { // 新增列表  
                    this.editText();
                } else if (num == 2) { // 编辑
                    if (this.dataFid != null) {
                        this.editText();
                    } else {
                        this.$message.warning("请先选择需要编辑的数据");
                    }
                } else if (num == 3) {//提交
                    this.$message.warning("该功能暂未开放");

                    // if (this.dataFid != null) {
                    //     this.yj_submit(0)
                    // } else {
                    //     this.$message.warning("请先选择需要提交的数据");
                    // }
                } else if (num == 4) {//删除
                    this.$message.warning("该功能暂未开放");

                    // if (this.dataFid != null) {
                    //     this.yj_delete(0)
                    // } else {
                    //     this.$message.warning("请先选择需要提交的数据");
                    // }
                } else if (num == 5) {//新增-监测
                    if (this.dataFid != null) {
                        this.editText1();
                    } else {
                        this.$message.warning("请先选择需要提交的数据");
                    }
                } else if (num == 7) { //查看
                    if (this.dataFid != null) {
                        this.editText1();
                    } else {
                        this.$message.warning("请先选择需要查看的数据");
                    }
                }
            } else {
                this.$message.warning("该用户无权限");
            }
        },
        editText() {//列表数据
            this.isShow = true;
        },
        editText1() {//梁板数列表
            this.isShow1 = true;
        },
        // 查看
        // 点击查看
        yj_viewForm(row, label) {
            this.currentRow = row;
            this.dataFid = row.fId;
            this.statusNum = 7;

            if (label == "无人机监测次数") {
                if ((row.testNum - 0) == 0) {
                    this.$message.warning("暂无记录,请添加");
                } else {
                    this.yj_edit(7);
                }
            }
            if (label == "关键工程名称") {
                this.editText();

            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },
        // 获取列表数据
        yj_searchForm(data) {
            var obj = {
                fCompanyid: data.fCompanyid, //左侧选中组织机构id
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数
                userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
                modelType: data.modelType, //0-待办 1-已办 传2 表示列表页查询
            }
            this.loading = true;
            var that = this;
            getKeyInfoList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        that.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        // 删除
        yj_delete() {
            if (this.dataFid != "" && this.dataFid != null) {
                this.$confirm("您是否要永久删除此数据?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.loading = true;
                        this.isLock = true
                        deleteKeyInfo({
                            fId: this.dataFid,
                            // userId: this.userInfo.userId, //当前登陆用户ID
                            // userName: this.userInfo.realName, //当前登陆用户名称
                        })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.isLock = false
                                    this.loading = false;
                                    this.yj_searchForm(this.getList);
                                    this.$message.success("删除成功");
                                    this.dataFid = null

                                } else {
                                    this.isLock = false
                                    this.$message.error("删除失败", res.msg);
                                    this.loading = false;
                                }
                            })
                            .catch(() => {
                                console.log("删除失败");
                                this.loading = false;
                            });
                    })
                    .catch(() => {
                        this.$message("取消删除");
                    });
            } else {
                return this.$message.error("请先选择需要删除的数据");
            }
        },
        // 提交
        yj_submit() {
            if (this.currentRow != null) {
                if (this.dataFid != "" && this.dataFid != null) {
                    this.$confirm("是否继续提交?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            this.isLock = true
                            this.loading = true;
                            updateKeyInfoStatus({
                                fId: this.dataFid,
                                userId: this.userInfo.userId, //当前登录人id
                                userName: this.userInfo.realName //当前登录人姓名
                            })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.dataFid = null
                                        this.isLock = false
                                        this.yj_searchForm(this.getList);
                                        this.$message.success("提交成功");
                                    } else if (res.errcode == -2) {
                                        this.loading = false;
                                        this.dataFid = null
                                        this.isLock = false
                                        this.yj_searchForm(this.getList);
                                        this.$message.warning(res.msg);
                                    } else {
                                        this.isLock = false
                                        this.$message.error("提交失败", res.msg);
                                        this.loading = false;
                                        return false;
                                    }
                                })
                                .catch(() => {
                                    this.loading = false;
                                    console.log("提交失败");
                                });
                        })
                        .catch(() => {
                            this.$message("取消提交");
                        });
                } else {
                    return this.$message.error("请先选择需要提交的数据");
                }
            }
        },

    },
};